/**
 * 
 * 广州锐鹏计算机科技有限公司（www.ruipengkj.com）
 * 
 * 
 * Copyright (c) 2011-2012 RUIPENGKJ.Co.Ltd. All rights reserved.
 * 
 * */
package com.ruipengkj.view.baseDataView;
/**
 * 说明：
 * 套餐面板
 * @Author：黄盛强
 * @Date：2012-03-26
 */
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.util.ArrayList;
import java.util.List;

import javax.swing.DefaultCellEditor;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.TableCellEditor;

import com.ruipengkj.bean.TCommodity;
import com.ruipengkj.bean.TCommoditySuitDetail;
import com.ruipengkj.commons.BuildSpringApplication;
import com.ruipengkj.commons.util.UUID;
import com.ruipengkj.commons.util.Util;
import com.ruipengkj.component.RPJButtonRoundBlue;
import com.ruipengkj.component.RPJButtonRoundOrager;
import com.ruipengkj.component.comboBox.CommodityJComboBoxTableComponent;
import com.ruipengkj.model.DataCommodityPackageModel;
import com.ruipengkj.service.TCommodityService;
import com.ruipengkj.service.TCommoditySuitDetailService;
import com.ruipengkj.view.base.BasePanel;

public class DataCommodityPackagePanel extends BasePanel{

	private static final long serialVersionUID = 1L;
	
	private JButton addButton; //添加行
	private JButton delButton; //删除行
	
	private JTable table;
	private JScrollPane tableJsp;
	private DataCommodityPackageModel tableModel;
	
	private CommodityJComboBoxTableComponent cbox;
	
	private TCommoditySuitDetailService tCommoditySuitDetailService = BuildSpringApplication.getInstance().getTCommoditySuitDetailServiceImpl();
	
	private List<TCommodity> delCommodityList = new ArrayList<TCommodity>();
	private String commodityID;
	private boolean isNew = false;
	
	public DataCommodityPackagePanel() {
		this.setLayout(new BorderLayout());
		this.init();
		this.initListener();
	}
	private void init() {
		tableModel = new DataCommodityPackageModel(delCommodityList);
		table=uiFactory.createRPJTable(tableModel, this, this, 30, JLabel.CENTER);
		cbox = new CommodityJComboBoxTableComponent();
		cbox.setBorder(null);
		TableCellEditor edt = new DefaultCellEditor(cbox);
		table.getColumnModel().getColumn(0).setCellEditor(edt);
		table.getColumnModel().getColumn(1).setCellEditor(edt);
		table.getColumnModel().getColumn(2).setCellEditor(edt);
		tableJsp=uiFactory.createJScrollPane(table);
		
		JPanel main_jp=new JPanel(new BorderLayout());
		main_jp.setBackground(Util.PANEL_COLOR);
		main_jp.add(tableJsp, BorderLayout.CENTER);
		main_jp.add(this.initTail(), BorderLayout.SOUTH);
		this.add(main_jp);
	}
	//底部
	private JPanel initTail() {
		
		JPanel tail_panel = uiFactory.createFlowJPanel(FlowLayout.LEFT);
		addButton = new RPJButtonRoundBlue("添加行");
		delButton = new RPJButtonRoundOrager("删除行");
		tail_panel.add(addButton);
		tail_panel.add(delButton);
		
		return tail_panel;
	}
	
	//添加事件监听器
	private void initListener() {
		addButton.addActionListener(this);
		delButton.addActionListener(this);
	}
	
	public void showDialog(String commodityID, boolean isNew) {
		this.commodityID = commodityID;
		this.isNew = isNew;
		if (!isNew && commodityID != null && !commodityID.equals("")) {
			TCommodityService tCommodityService = BuildSpringApplication.getInstance().getTCommodityServiceImpl();
			try {
				List<TCommoditySuitDetail> list = tCommoditySuitDetailService.findService(
						new TCommoditySuitDetail(null, null, new TCommodity(commodityID)));
				for (int i = list.size() - 1; i >= 0; --i) {
					TCommodity tCommodity = tCommodityService.findByIdService(list.get(i).getSubCommodityID());
					if (tCommodity != null && tCommodity.getCommodityID() != null) {
						tCommodity.setSuitDetailsID(list.get(i).getId());
						tableModel.addRow(tCommodity);
					}
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}
	
	@Override
	public void actionPerformed(ActionEvent e) {
		if(e.getSource() == addButton) {
			//添加行
			tableModel.addRow(new TCommodity());
		} else if(e.getSource() == delButton) {
			//删除行
			if(table.getRowCount()==0){
				JOptionPane.showMessageDialog(null, "当前数据为空");
				return;
			}
			int selects[] = table.getSelectedRows();
			for (int i = selects.length-1; i >= 0; --i) {
				if (!isNew) {//当时新数据时，不用添加删除行
					TCommodity tCommodity = tableModel.getTCommodity(i);
					if (tCommodity.getSuitDetailsID() != null) {
						delCommodityList.add(tCommodity);
					}
				}
				tableModel.deleteRow(i);
			}
		}
	}
	
	//检测table中是否有数据/数据是否正确
	public boolean check() {
		List<TCommodity> tCommodityList = tableModel.getTCommodityList();
		for	(int i = tCommodityList.size() - 1; i >= 0; --i) {
			if (tCommodityList.get(i).getCommodityID() != null) {
				return true;
			}
		}
		return false;
	}
	
	public boolean save() {
		List<TCommodity> tCommodityList = tableModel.getTCommodityList();
		try {
			//删除旧数据
			if (!isNew)
			for (int i = delCommodityList.size() - 1; i >= 0; --i) {
				tCommoditySuitDetailService.deleteService(delCommodityList.get(i).getSuitDetailsID());
			}
			//添加数据
			for	(int i = tCommodityList.size() - 1; i >= 0; --i) {
				if (tCommodityList.get(i).getCommodityID() != null) {
					if (tCommodityList.get(i).getSuitDetailsID() == null)
					tCommoditySuitDetailService.saveOrUpdateService(new TCommoditySuitDetail(UUID.GenUUID15()
							, tCommodityList.get(i).getCommodityID(), new TCommodity(commodityID)));
				}
			}
		} catch (Exception e) {
			Util.showMessageDialog("保存失败！");
			e.printStackTrace();
			return false;
		}
		return true;
	}
	@Override
	public void clear() {
		
	} 
}